package com.lyj.hw_student.system.controller;

import com.lyj.hw_student.common.Result;
import com.lyj.hw_student.system.entity.Course;
import com.lyj.hw_student.system.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService iCourseService;

    /**新增课程信息*/
    @PostMapping ("/addCourse")
    public String addCourse(@RequestBody Course course){
        iCourseService.save(course);
        return "success";
    }

    // 查询单个用户（使用路径参数优化）
    @GetMapping("/search")
    public List<Course> searchCourse(@RequestParam("courseName") String courseName) {
        return iCourseService.lambdaQuery()
                .like(Course::getCourseName, courseName)
                .list();
    }




    // 获取所有课程信息（保持不变）
    @GetMapping("/getAllCourses")
    public List<Course> getAllUsers() {
        return iCourseService.list();
    }

    /**删除课程信息*/
    @DeleteMapping("/delCouse/{courseNo}")
    public String delCourse(@PathVariable Integer courseNo){
        iCourseService.removeById(courseNo);
        return "success";
    }

    /**修改课程信息*/
    @PutMapping("/updateCourse")
    public String updateCourse(@RequestBody Course course){
        iCourseService.updateById(course);
        return "success";
    }

    /**查询课程信息*/
    @GetMapping("/selectCourse/{courseNo}")
    public Course selectCourse(@PathVariable Integer courseNo){
        Course course = iCourseService.getById(courseNo);
        return course;
    }

    @GetMapping("/getAll")
    public List<Course> getAllCourses() {
        return iCourseService.list(); // 确保Service层有数据
    }
}